package org.wbd.pklib.web.beans;

import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.wbd.pklib.db.Repo;
import org.wbd.pklib.db.User;
import org.wbd.pklib.db.logic.LogicRespond;
import org.wbd.pklib.db.logic.RepoLogic;
import org.wbd.pklib.db.logic.UserLogic;

public class RepoBean {
	private User user;
	private String name;
	private ArrayList<Repo> avalibleRepos;
	private Repo selectedRepo;
	private ArrayList<Repo> userRepos;
	
	
	
	public ArrayList<Repo> getUserRepos() {
		return userRepos;
	}
	public void setUserRepos(ArrayList<Repo> userRepos) {
		this.userRepos = userRepos;
	}
	public ArrayList<Repo> getAvalibleRepos() {
		return avalibleRepos;
	}
	public void setAvalibleRepos(ArrayList<Repo> avalibleRepos) {
		this.avalibleRepos = avalibleRepos;
	}
	public Repo getSelectedRepo() {
		return selectedRepo;
	}
	public void setSelectedRepo(Repo selectedRepo) {
		this.selectedRepo = selectedRepo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	public String create(){
		RepoLogic logic= RepoLogic.getInstance();
		LogicRespond respond= logic.createRepo(user, name);
		
		if (respond.isSuccesful()) {
			FacesMessage message = new FacesMessage("Repository created!");
			FacesContext.getCurrentInstance().addMessage(null, message);
			UserBean bean= (UserBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userBean");
			bean.createUserRepoList();
			bean.createAvailableRepos();
		} else {
			FacesMessage message = new FacesMessage("Repository creation failed!");
			FacesContext.getCurrentInstance().addMessage(null, message);
		}
		return "menu?faces-redirect= true";
	}
	
	public String join(){
		RepoLogic logic= RepoLogic.getInstance();
		LogicRespond respond= logic.joinRepo(user, selectedRepo);
		if (respond.isSuccesful()) {
			FacesMessage message = new FacesMessage("Join sucessful!");
			FacesContext.getCurrentInstance().addMessage(null, message);
			UserBean bean= (UserBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userBean");
			bean.getUserRepoList().add(selectedRepo);
			bean.getAvailableRepos().remove(selectedRepo);
		} else {
			FacesMessage message = new FacesMessage("Join failed!");
			FacesContext.getCurrentInstance().addMessage(null, message);
		}
		return "menu?faces-redirect= true";
	}
	
	public String leave(){
		UserLogic logic= UserLogic.getInstance();
		LogicRespond respond = logic.leaveRepo(user, selectedRepo.getRepoId());
		if (respond.isSuccesful()) {
			FacesMessage message = new FacesMessage("You have left from repository!");
			FacesContext.getCurrentInstance().addMessage(null, message);
			UserBean bean= (UserBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userBean");
			bean.getUserRepoList().remove(selectedRepo);
			bean.getAvailableRepos().add(selectedRepo);
	
		} else {
			FacesMessage message = new FacesMessage("Leaving failed!");
			FacesContext.getCurrentInstance().addMessage(null, message);
		}
		return "menu?faces-redirect= true";
	}
	
}
